﻿using UnityEngine;
using System.Collections;
using System.Collections.Generic;
using LitJson;

[AddComponentMenu("PUEX/Emitter/SphereEmitter")]
public class PUEXSphereEmitter : PUEXEmitter {

	#region - Editable property
	[Header("SphereEmitter:")]
	public float radius;
	public bool emitOnSurface;
	public bool emitLinear;
	public bool flipLinear;
	public bool alignDirection;
	public bool tangentDirection;
	public float randomDeviant;
	public bool flipDirection;
	#endregion

	#region - Property
	protected Vector3 _emitOffset;
	protected float _emitDist;
	#endregion

	#region - Lifecycle
	public PUEXSphereEmitter ()
	{
		radius = 20f;
		emitOnSurface = false;
		emitLinear = false;
		flipLinear = false;
		alignDirection = true;
		tangentDirection = false;
		flipDirection = false;
		randomDeviant = 0f;
	}
	#endregion

	#region - Emit
	public override void emitParticles(int requested)
	{
		if (requested > 0 && emitLinear) {
			_emitDist = Mathf.Ceil (Mathf.Sqrt (requested));
		}

		base.emitParticles(requested);
	}

	protected override void initParticlePosition (PUEXParticle particle, int index, int requested)
	{
		if (emitLinear) {
			float r = timeFraction * radius;
			if (flipLinear)
				r = (1f - timeFraction) * radius;

			int v = (int)_emitDist;
			float a = ((index / v) + 0.5f) / _emitDist * Mathf.PI * 2f;
			float b = Mathf.Asin(((index % v) + 0.5f) / _emitDist * 2f - 1f);
			float x = Mathf.Cos (a) * Mathf.Cos (b);
			float y = Mathf.Sin (a) * Mathf.Cos (b);
			float z = Mathf.Sin (b);

			_emitOffset = new Vector3(x, y, z) * r;
		} else {
			float r = radius;
			if (emitOnSurface == false)
				r = Random.value * radius;

			_emitOffset = Random.insideUnitSphere.normalized * r;
		}
		particle.position = particle.originalPosition = emitterToSimulationTransform.MultiplyPoint (_emitOffset);
	}

	protected override void initParticleVelocity (PUEXParticle particle, int index, int requested)
	{
		if (alignDirection) {
			Vector3 dir = _emitOffset;

			if (tangentDirection) {
				dir = Vector3.Cross(dir, Vector3.forward);
			}

			if (randomDeviant > 0f) {
				dir = PUEXUtils.randomDeviant(dir, randomDeviant);
			}
			
			if (flipDirection) {
				dir *= -1f;
			}

			particle.direction = particle.originalDirection = emitterToSimulationTransform.MultiplyVector (dir).normalized;
		} else {
			particle.direction = particle.originalDirection = emitterToSimulationTransform.MultiplyVector (Random.insideUnitSphere).normalized;
		}

		particle.speed = particle.originalSpeed = startSpeed.getValue (timeFraction);
	}
	#endregion
	
	#region - Draw
	public void OnDrawGizmos()
	{
		if (!enabled)
			return;
		
		Matrix4x4 defaultMatrix = Gizmos.matrix;
		Gizmos.matrix = transform.localToWorldMatrix;
		
		Color defaultColor = Gizmos.color;
		Gizmos.color = Color.blue;

		Gizmos.DrawWireSphere (Vector3.zero, radius);
		
		Gizmos.color = defaultColor;
		Gizmos.matrix = defaultMatrix;
	}
	#endregion

	#region - Serialization
	public override JsonData save (JsonData systemData)
	{
		JsonData ret = base.save (systemData);
		
		ret ["radius"] = radius;
		ret ["emit_on_surface"] = emitOnSurface;
		ret ["emit_linear"] = emitLinear;
		ret ["flip_linear"] = flipLinear;
		ret ["align_direction"] = alignDirection;
		ret ["tangent_direction"] = tangentDirection;
		ret ["random_deviant"] = randomDeviant;
		ret ["flip_direction"] = flipDirection;
		
		return ret;
	}
	#endregion

}
